import axios from "axios";
import { defaultConfig } from "./user";
import { sleepRand } from "../utils/time";
import type { lotteryInfo } from "./type";
defaultConfig.baseURL = "https://api.icitybox.cn/api";
const http = axios.create(defaultConfig);

export const unJoinLotteryIds = async (): Promise<string[]> => {
  await sleepRand(0, 1500);
  let info = await http.post("/lottery/lottery_info", { up_type: 1 });
  let ids: string[] = [];
  info.data?.return_data?.map(
    (item: { is_join: number; lottery_id: string; pay_modou: number }) => {
      if (item.is_join == 0 && item.pay_modou <= 20) {
        ids.push(item.lottery_id);
      }
    }
  );
  return ids;
};

export const joinLottery = async (loggeryId: number) => {
  await sleepRand(1000, 3500);
  await http.post("/lottery/user_join_lottery", {
    lottery_id: loggeryId,
  });
};

export const unReceiveLottery = async (): Promise<lotteryInfo[]> => {
  await sleepRand(2000, 3000);
  let hitLoggery = await http.post("/lottery/user_lottery_log", {
    up_status: 2,
  });
  let list: lotteryInfo[] = [];
  hitLoggery.data?.map((item: lotteryInfo) => {
    if (item.receive_date == "" && item.expire_state !== 2) {
      list.push(item);
    }
  });
  return list;
};
